<?php

/**
 * CRMUsers main page
 * @package ModuleAdministratif
 * @version 1.0b
 */

session_start();

// Configuration file
include 'configuration.php';
// Functions
include 'inc/functions.php';

// Checking if user is logged in
if (!CheckSession()):
	header('Location: index.php');
	exit();
endif;

// Include all objects
foreach (glob($configuration['webroot'].'objects/*.php') as $class) { include_once $class; }

// ---------- START: HEAD
include $configuration['webroot'].'inc/head.php';
// ---------- END: HEAD

// ---------- START: MENU
include $configuration['webroot'].'inc/menu.php';
// ---------- END: MENU

// ---------- START: BODY ?>

<div id="title" style="background-image:url('css/img/users.png')">Utilisateurs</div>

<?php 

if (!isset($_GET['operation'])):

	if (empty($_SESSION['search']['crmusers']))
		$params = null;
	else 
		$params = Array(Array('login', 'LIKE', '%'.$_SESSION['search']['crmusers'].'%'));
	
	$crmuser = new CRMUser();
	$crmuserList = $crmuser->GetList($params); ?>
	
	<div id="nav">
		<a href="#" id="details" class="nav">Détails</a>
		<span class="separator"></span>
		<a href="crmusers.php?operation=add" id="add" class="nav">Nouveau</a>
		<a href="#" id="delete" class="nav">Supprimer</a>
		<span class="separator"></span>
		<input type="text" id="search-field" class="search-field" value="<?php echo (!empty($_SESSION['search']['crmusers'])) ? $_SESSION['search']['crmusers'] : ''; ?>" />
		<a href="crmusers" id="search" class="search">Rechercher</a>
	</div>
	
	<table cellpadding="0" cellspacing="0" border="0" class="table-objects">
		<tr>
			<th class="check"></th>
			<th>#</th>
			<th>Login</th>
			<th>Rôle</th>
			<th>Contact</th>
		</tr>
		<?php foreach ($crmuserList as $crmuser) {
			$credentials = $crmuser->GetCredentials();
			$contact = $crmuser->GetContact();
			
			$res  = '<tr>';
			$res .= '<td class="check"><input type="checkbox" value="'.$crmuser->crmuserId.'" /></td>';
			$res .= '<td>'.$crmuser->crmuserId.'</td>';
			$res .= '<td><strong>'.$crmuser->login.'</strong></td>';
			$res .= '<td>'.$credentials->name.'</td>';
			$res .= '<td><a href="contacts.php?id='.$contact->contactId.'&operation=edit">'.$contact->lastname.' '.$contact->firstname.'</a></td>';
			$res .= '</tr>';
			echo $res;
		} ?>
	</table>
	
	<div id="sub-nav">
		<a href="#" class="selectall">Tout cocher</a>
		<a href="#" class="selectnone">Tout décocher</a>
	</div>
	
<?php
else:
	$crmuser = new CRMUser();
	$contact = new Contact();
	$credentials = new Credentials();
	
	// In case we are in an update state
	if (($_GET['operation'] == 'edit') && isset($_GET['id'])):
		$crmuser->Get($_GET['id']);
		$contact = $crmuser->GetContact();
		$credentials = $crmuser->GetCredentials();
	endif;

	include 'forms/crmuser.php';

endif;
// ---------- END: BODY

// ---------- START: FOOT
include $configuration['webroot'].'inc/foot.php';
// ---------- END: FOOT

// ---------- JAVASCRIPTS AND END
include $configuration['webroot'].'inc/js.php'; ?>
<script type="text/javascript" src="js/crmusers.js"></script>

</body>
</html>